package com.szpt.TrainingAssistant.sync;

import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.szpt.TrainingAssistant.sql.TrainingRecord;

public class Encoder {
	private JSONObject mJson;

	public Encoder(String user, String pwd) {
		mJson = new JSONObject();

		try {
			mJson.put("user", user);
			mJson.put("pwd", pwd);
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	public void addRecords(List<TrainingRecord> records) {
		JSONArray JsonRecords = new JSONArray();
		for (TrainingRecord record : records) {
			JSONArray JsonRecord = new JSONArray();
			JsonRecord.put(record.getType()).put(record.getResult()).put(record.getStarttime());
			JsonRecords.put(JsonRecord);
		}
		try {
			mJson.put("records", JsonRecords);
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}
	
	public String toString() {
		return mJson.toString();
	}
}
